package com.hemaapp.yjnh.cart;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.EmptyViewHolder;
import com.hemaapp.yjnh.bean.Cart;
import com.hemaapp.yjnh.listener.CartListener;
import com.hemaapp.yjnh.utils.ImageUtils;
import com.hemaapp.yjnh.view.NumberSelectorLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import xtom.frame.util.XtomToastUtil;

/**
 * 类描述：
 * 创建人：GuiyouZhang
 * 创建时间：2016/7/14 15:20
 * 修改备注：
 */
public class ShoppingCartAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<Cart.ChildItem> cartChildren;
    private Context mContext;
    private CartListener listener;
    private List<SwipeItemLayout> mOpenedSil = new ArrayList<>();//当前处于打开状态的item

    public ShoppingCartAdapter(Context context, ArrayList<Cart.ChildItem> cartChildren) {
        this.mContext = context;
        this.cartChildren = cartChildren;
    }

    public void setListener(CartListener listener) {
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        if (cartChildren.size() == 0)
            return -1;
        else
            return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.listitem_empty_base, parent, false);
            return new EmptyViewHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_shopping_cart, parent, false);
            return new ViewHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (cartChildren.size() == 0) {
            if (holder instanceof EmptyViewHolder)
                ((EmptyViewHolder) holder).tvEmtpy.setText("您还没有往购物车里添加商品");
        } else {
            if (holder instanceof ViewHolder) {
                ((ViewHolder) holder).setDatas(position);
            }
        }
    }

    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (SwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }

    @Override
    public int getItemCount() {
        return cartChildren.size() == 0 ? 1 : cartChildren.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.layout_shop)
        LinearLayout layoutShop;
        @Bind(R.id.ckbx_shop_name)
        CheckBox ckbxShopName;
        @Bind(R.id.iv_select)
        ImageView ivSelect;
        @Bind(R.id.iv_goods)
        ImageView ivGoods;
        @Bind(R.id.tv_goods_name)
        TextView tvGoodsName;
        @Bind(R.id.tv_goods_price)
        TextView tvGoodsPrice;
        @Bind(R.id.tv_goods_spec)
        TextView tvGoodsSpec;
        @Bind(R.id.layout_number_selector)
        NumberSelectorLayout numberSelector;
        @Bind(R.id.layout_front)
        LinearLayout layoutFront;
        @Bind(R.id.tv_cart_item_delte)
        TextView tvCartItemDelte;
        @Bind(R.id.item_contact_swipe_root)
        SwipeItemLayout swipeRoot;

        Cart.ChildItem childItem;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            layoutFront.setOnClickListener(this);
            tvCartItemDelte.setOnClickListener(this);
            ivSelect.setOnClickListener(this);
            ckbxShopName.setOnClickListener(this);
            numberSelector.setMinValue(1);
            numberSelector.setNumberSelectorListener(new NumberSelectorLayout.NumberSelectorListener() {
                @Override
                public void onNumberChanged(int curValue) {
                    if (listener != null && childItem != null)
                        listener.modifyGoodsCount(childItem, curValue+"");
                }
            });
            swipeRoot.setSwipeAble(true);
            swipeRoot.setDelegate(new SwipeItemLayout.SwipeItemLayoutDelegate() {
                @Override
                public void onSwipeItemLayoutOpened(SwipeItemLayout swipeItemLayout) {
                    closeOpenedSwipeItemLayoutWithAnim();
                    mOpenedSil.add(swipeItemLayout);
                }

                @Override
                public void onSwipeItemLayoutClosed(SwipeItemLayout swipeItemLayout) {
                    mOpenedSil.remove(swipeItemLayout);
                }

                @Override
                public void onSwipeItemLayoutStartOpen(SwipeItemLayout swipeItemLayout) {
                    closeOpenedSwipeItemLayoutWithAnim();
                }
            });
        }

        public void setDatas(int position) {
            Cart.ChildItem item = cartChildren.get(position);
            this.childItem = item;
            if (position == 0)
                layoutShop.setVisibility(View.VISIBLE);
            else {
                String shop1 = cartChildren.get(position - 1).getSeller_id();
                String shop2 = cartChildren.get(position).getSeller_id();
                if (shop1.equals(shop2))
                    layoutShop.setVisibility(View.GONE);
                else {
                    layoutShop.setVisibility(View.VISIBLE);
                }
            }
            ckbxShopName.setText(item.getSeller_name());
            if (item.isSellerChecked())
                ckbxShopName.setChecked(true);
            else
                ckbxShopName.setChecked(false);
            if (item.isChecked())
                ivSelect.setImageResource(R.drawable.select_y_orange);
            else
                ivSelect.setImageResource(R.drawable.select_n);
            ImageUtils.loadSmalImage(mContext, item.getImgurl(), ivGoods);
            tvGoodsName.setText(item.getName());
            tvGoodsPrice.setText("¥" + item.getPrice());
            if (!TextUtils.isEmpty(item.getOne_specname())) {
                tvGoodsSpec.setVisibility(View.VISIBLE);
                StringBuffer spec = new StringBuffer(item.getOne_spec());
                spec.append(":").append(item.getOne_specname());
                if (!TextUtils.isEmpty(item.getTwo_specname())) {
                    spec.append("    ").append(item.getTwo_spec()).append(":").append(item.getTwo_specname());
                }
                tvGoodsSpec.setText(spec.toString());
            } else
                tvGoodsSpec.setVisibility(View.GONE);
            numberSelector.setMaxValue(9999999);
            int buyCount = 1;
            try {
                buyCount = Integer.parseInt(item.getBuycount());
            }catch (Exception e) {
                e.printStackTrace();
            }
            numberSelector.setNumber(buyCount);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_cart_item_delte:
                    if (listener != null && childItem != null)
                        listener.deleteCartItem(childItem);
                    break;
                case R.id.layout_front:
                    if (listener != null && childItem != null) {
                        listener.onItemClickListener(childItem);
                    }
                    break;
                case R.id.iv_select:
                    if (listener != null && childItem != null)
                        listener.onItemCheckChangedListener(childItem, !childItem.isChecked());
                    break;
                case R.id.ckbx_shop_name:
                    if (listener != null && childItem != null)
                        listener.onHeaderClickListener(childItem.getSeller_id());
                    break;
            }
        }
    }
}
